<?php
            
use app\model\Util; 
use app\model\Theme; 



/**
 * Desc:
 * Author: wp
 * Time: 2022/5/20 4:31 下午
 * Email: 1565602678@qq.com
 */
class Call_Page extends PluginWebPage
{
    function main($method)
    {
        $method = 'call_group';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $list = WeikeModel::getCallGiftGroups($themeid,false,false);
        $return_clicent = get_defined_vars();
return $this->view('/weike/call/main', $return_clicent);
    }

    function addpage(){
        $method = 'call_group_add';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $call_group_id = intval($_P['call_group_id'])?:0;
        $data = [];
        if($call_group_id){
            $data = WeikeModel::getCallGroupInfoById($call_group_id,$themeid);
        }
        $return_clicent = get_defined_vars();
return $this->view('/weike/call/edit', $return_clicent);
    }

    function addinfo(){
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $call_group_id = intval($_P['call_group_id']);
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        $title = trim($_P['title'])?:'';
        $status = intval($_P['status']);
        if (empty($title)){
            show_json(0,'礼品分组名称不能为空！');
        }
        //组装数组
        $data = [
            'title'=>$title,
            'status'=>$status,
        ];
        if($call_group_id){
            //编辑
            //根据ID查找信息
            $info = WeikeModel::getCallGroupInfoById($call_group_id,$themeid);
            if(empty($info)){
                show_json(0,'未找到礼品分组信息');
                return;
            }
            $where = ['id'=>$call_group_id,'themeid'=>$themeid];
            //修改数据库字段
            $res = WeikeModel::editCallGroupDataByWhere($where,$data);
        }else{
            //新增
            //存入数据库
            $data['themeid'] = $themeid;
            $res = WeikeModel::insertCallGroupData($data);

        }
        if (!$res){
            show_json(0,'操作失败');
        }else{
            show_json(1,'操作成功');
        }
    }

    function giftmain(){
        global $_G,$_P,$_S,$_C;
        $method = 'list';
        $do = 'weike';
        $system = $_S;
        $config = $_C;
        $call_group_id = intval($_P['call_group_id']);
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        //查找分组下礼品列表
        $list = WeikeModel::getCallGiftList($call_group_id,false);
        $return_clicent = get_defined_vars();
return $this->view('/weike/call/giftmain', $return_clicent);
    }

    function addgift(){
        $method = 'add_gift';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $call_group_id = intval($_P['call_group_id'])?:0;
        if (!$call_group_id){
            show_json(0,'礼品分组参数错误');
            return;
        }
        $gift_id = intval($_P['gift_id']);
        $data = [];
        $content = '';
        if($gift_id){
            $data = WeikeModel::getCallGiftInfoById($call_group_id,$gift_id);
            $content = $data['content'];
        }
        $return_clicent = get_defined_vars();
return $this->view('/weike/call/giftedit', $return_clicent);
    }

    function addgiftinfo(){
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $call_group_id = intval($_P['call_group_id']);
        $themeid = intval($_P['themeid']);
        $gift_id = intval($_P['gift_id'])?:0;
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        if (!$themeid){
            show_json(0,'活动入口不正确');
            return;
        }
        $title = trim($_P['title'])?:'';
        $status = intval($_P['status']);
        $num_limit = intval($_P['num_limit']);
        if ($num_limit < 1){
            show_json(0,'打call数量必须大于0！');
            return;
        }
        if (empty($_P['content'])){
            show_json(0,'请上传照片！');
            return;
        }
//        if (count($_P['content']) > 3){
//            show_json(0,'最多上传3张照片！');
//            return;
//        }
        if (empty($title)){
            show_json(0,'礼品名称不能为空！');
            return;
        }
        //处理图片
        $photos = tomedia($_P['content']);;
//        foreach ($_P['content'] as $v){
//            $photos[]=tomedia($v);
//        }
        //组装数组
        $data = [
            'title'=>$title,
            'status'=>$status,
            'content'=>$photos,
            'num_limit'=>$num_limit
        ];
        if($gift_id){
            //编辑
            //根据ID查找信息
            $info = WeikeModel::getCallGiftInfoById($call_group_id,$gift_id);
            if(empty($info)){
                show_json(0,'未找到礼品信息');
                return;
            }
            $where = ['id'=>$gift_id,'group_id'=>$call_group_id];
            //修改数据库字段
            $res = WeikeModel::editCallGiftDataByWhere($where,$data);
        }else{
            //新增
            //存入数据库
            $data['group_id'] = $call_group_id;
            $res = WeikeModel::insertCallGiftData($data);
        }
        if (!$res){
            show_json(0,'操作失败');
        }else{
            show_json(1,'操作成功');
        }
    }

    function listinfo(){
        $method = 'call_list';
        $do = 'weike';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        $wk_id = intval($_P['wk_id']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $count = WeikeModel::getCallRecordList($themeid,$wk_id,$search_data,0,0);
        $total = $page_total = $count['count_num'];
        if(checksubmit('export_submit', true)){
            $page_limit = 2000;
            $pagesize = ceil($total/$page_limit);

            $header = array(
                'name'=>'客户姓名','nickname' => '打call好友昵称','mobile' => '打call好友电话',
                'created_at' => '打call时间'
            );
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";
//            for ($j = 1; $j <= $pagesize; $j++) {
                $ex_page = $_P['ex_page']?$_P['ex_page']:1;
                $list = WeikeModel::getCallRecordList($themeid,$wk_id,'',($ex_page-1)*$page_limit,$page_limit);
                if (!empty($list)) {
                    $size = ceil(count($list) / 500);
                    for ($i = 0; $i < $size; $i++) {
                        $buffer = array_slice($list, $i * 500, 500);
                        foreach ($buffer as $row) {
                            foreach ($keys as $key) {
                                $data[] = $row[$key];
                            }
                            if (isset($data)){
                                $user[] = implode("\t ,", $data) . "\t ,";
                                unset($data);
                            }
                        }
                        $html .= implode("\n", $user)."\n";
                        unset($user);
                    }
              //  }
            }
            //新版本导出合并
            $is_end = 0;
            if($ex_page>=$pagesize){
                $is_end = 1;
            }
            Util::WxJump($_S,$html,'打call记录',$themeid,$is_end,$pagesize);
        }
        $list = WeikeModel::getCallRecordList($themeid,$wk_id,$search_data,$page_start,$page_size);
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/weike/call/list', $return_clicent);
    }
}